/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Bussiness;

import DatabaseAccess.DBHelper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

/**
 *
 * @author user
 */
public class KhachHang {
    private String MaKH;
    private String TenKH;
    private String GioiTinh;
    private String DiaChi;
    private String DienThoai;
    DBHelper conn = new DBHelper();

    /**
     * @return the MaKH
     */
    public String getMaKH() {
        return MaKH;
    }

    /**
     * @param MaKH the MaKH to set
     */
    public void setMaKH(String MaKH) {
        this.MaKH = MaKH;
    }

    /**
     * @return the TenKH
     */
    public String getTenKH() {
        return TenKH;
    }

    /**
     * @param TenKH the TenKH to set
     */
    public void setTenKH(String TenKH) {
        this.TenKH = TenKH;
    }

    /**
     * @return the GioiTinh
     */
    public String getGioiTinh() {
        return GioiTinh;
    }

    /**
     * @param GioiTinh the GioiTinh to set
     */
    public void setGioiTinh(String GioiTinh) {
        this.GioiTinh = GioiTinh;
    }

    /**
     * @return the DiaChi
     */
    public String getDiaChi() {
        return DiaChi;
    }

    /**
     * @param DiaChi the DiaChi to set
     */
    public void setDiaChi(String DiaChi) {
        this.DiaChi = DiaChi;
    }

    /**
     * @return the DienThoai
     */
    public String getDienThoai() {
        return DienThoai;
    }

    /**
     * @param DienThoai the DienThoai to set
     */
    public void setDienThoai(String DienThoai) {
        this.DienThoai = DienThoai;
    }
    public KhachHang(){}
    public KhachHang(String MaKH, String TenKH, String GioiTinh, String DiaChi, String DienThoai){
        this.setMaKH(MaKH);
        this.setTenKH(TenKH);
        this.setGioiTinh(GioiTinh);
        this.setDiaChi(DiaChi);
        this.setDienThoai(DienThoai);
    }
    public int insertKhachHang(KhachHang a) throws SQLException
    {
        int i = 0;
        try{
            Vector v = new Vector();
            v.add(a.getMaKH());
            v.add(a.getTenKH());
            v.add(a.getGioiTinh());
            v.add(a.getDiaChi());
            v.add(a.getDienThoai());
            i=conn.executeUpdate("sp_InsertKhachHang",v);
        }catch(SQLException ex){}
        return i;
    }
    public int updateKhachHang(KhachHang a) throws SQLException
    {
        DBHelper obj =new DBHelper();
        int i = 0;
        try{
            Vector v = new Vector();
            v.add(a.getMaKH());
            v.add(a.getTenKH());
            v.add(a.getGioiTinh());
            v.add(a.getDiaChi());
            v.add(a.getDienThoai());
            i=obj.executeUpdate("sp_UpdateKhachHang",v);
        }catch(SQLException ex){}
        return i;
    }
    public int DelKhachHang(String MaKH) throws SQLException
    {
        int i=0;
        try
        {
            Vector paramList=new Vector();
            paramList.add(MaKH);            
            i=conn.executeUpdate("sp_DelKhachHang",paramList);
        }
        catch(SQLException sqlex)
        {
            
        }
        return i;
    }
    public Vector getAllKhachHang() throws SQLException
    {
        Vector v = new Vector();
        try 
        {
            ResultSet rs = conn.executeQuery("sp_getAllKhachHang");
            while(rs.next())
            {                  
                KhachHang b = new KhachHang();
                b.setMaKH(rs.getString(1));
                b.setTenKH(rs.getString(2));
                b.setGioiTinh(rs.getString(3));
                b.setDiaChi(rs.getString(4));
                b.setDienThoai(rs.getString(5));
                v.add(b);                         
            }
        } 
        catch(SQLException sqle) 
        {            
        }
        return v;
    }
    public Vector getKhachHang_MaKH(String MaKH) throws SQLException
    {
        Vector v = new Vector();
        Vector v1 = new Vector();
        v1.add(MaKH);
        try{
            ResultSet rs = conn.executeQuery("sp_getMaKH", v1);
            while(rs.next()){
                KhachHang b = new KhachHang();
                b.setMaKH(rs.getString(1));
                b.setTenKH(rs.getString(2));
                b.setGioiTinh(rs.getString(3));
                b.setDiaChi(rs.getString(4));
                b.setDienThoai(rs.getString(5));
                v.add(b);
            }
        }catch(SQLException sqle) {}
        return v; 
    }
    public Vector getKhachHang_TenKH(String TenKH) throws SQLException
    {
        Vector v = new Vector();
        Vector v1 = new Vector();
        v1.add(TenKH);
        try{
            ResultSet rs = conn.executeQuery("sp_getTenKH", v1);
            while(rs.next()){
                KhachHang b = new KhachHang();
                b.setMaKH(rs.getString(1));
                b.setTenKH(rs.getString(2));
                b.setGioiTinh(rs.getString(3));
                b.setDiaChi(rs.getString(4));
                b.setDienThoai(rs.getString(5));
                v.add(b);
            }
        }catch(SQLException sqle) {}
        return v;
    }
}
